358C - Dima and Containers - CodeForces Solution


constructive algorithms greedy implementation *2000

Please click on ads to support us..

Python Code:

n = int(input())
last_zero, largest, nd_largest, rd_largest = -1, -1, -1, -1
arr = [int(input()) for _ in range(n)]
for i in range(n):
    if arr[i] == 0:
        filled_stk, filled_queue, filled_front = False, False, False
        for j in range(last_zero + 1, i):
            if arr[j] == largest and not filled_stk:
                filled_stk = True
                print('pushStack')
            elif arr[j] == nd_largest and not filled_queue:
                filled_queue = True
                print('pushQueue')
            elif arr[j] == rd_largest and not filled_front:
                filled_front = True
                print('pushFront')
            else:
                print('pushBack')
        print(int(filled_stk) + int(filled_queue) + int(filled_front), end='')
        if filled_stk:
            print(' popStack', end='')
        if filled_queue:
            print(' popQueue', end='')
        if filled_front:
            print(' popFront', end='')
        print('\n', end='')
        last_zero = i
        largest = nd_largest = rd_largest = -1
    else:
        if arr[i] >= largest:
            largest, nd_largest, rd_largest = arr[i], largest, nd_largest
        elif arr[i] >= nd_largest:
            nd_largest, rd_largest = arr[i], nd_largest
        elif arr[i] > rd_largest:
            rd_largest = arr[i]
for i in range(last_zero + 1, n):
    print('pushStack')

			 		    					 		 			 	 	 	 	

C++ Code:

#define _GLIBCXX_FILESYSTEM
#include <bits/stdc++.h>
using namespace std;

#define ll long long

int main()
{
	//freopen("reduce.in", "r", stdin);
    //freopen("reduce.out", "w", stdout);
	ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n,x,k=0;
	vector<pair<int,int>> v;
    cin>>n;
    string in[]={"pushStack","pushQueue","pushFront","pushBack"},out[]={"popStack","popQueue","popFront","popBack"};
    while (n--){
		cin>>x;
		if (x!=0) v.push_back({x,k++});
		else {
			if (k<=3){
				for (int i=0;i<k;i++) cout<<in[i]<<'\n';
				cout<<k;
				for (int i=0;i<k;i++) cout<<' '<<out[i];
				cout<<'\n';
				v.clear();
				k=0;
				continue;
			}
			sort(v.begin(),v.end());
			vector<int> id(k);
			for (int i=0;i<k;i++) id[v[i].second]=i;
			for (int i=0;i<k;i++){
				if (id[i]==k-3) cout<<in[2]<<'\n';
				else if (id[i]==k-2) cout<<in[1]<<'\n';
				else if (id[i]==k-1) cout<<in[0]<<'\n';
				else cout<<in[3]<<'\n';
			}
			cout<<3<<' '<<out[0]<<' '<<out[1]<<' '<<out[2]<<'\n';
			k=0;
			v.clear();
		}
    }
    while(k--) cout<<in[0]<<'\n';
}


Comments

Submit
0 Comments
More Questions

1832. Check if the Sentence Is Pangram
1678. Goal Parser Interpretation
1389. Create Target Array in the Given Order
1313. Decompress Run-Length Encoded List
1281. Subtract the Product and Sum of Digits of an Integer
1342. Number of Steps to Reduce a Number to Zero
1528. Shuffle String
1365. How Many Numbers Are Smaller Than the Current Number
771. Jewels and Stones
1512. Number of Good Pairs
672. Richest Customer Wealth
1470. Shuffle the Array
1431. Kids With the Greatest Number of Candies
1480. Running Sum of 1d Array
682. Baseball Game
496. Next Greater Element I
232. Implement Queue using Stacks
844. Backspace String Compare
20. Valid Parentheses
746. Min Cost Climbing Stairs
392. Is Subsequence
70. Climbing Stairs
53. Maximum Subarray
1527A. And Then There Were K
1689. Partitioning Into Minimum Number Of Deci-Binary Numbers
318. Maximum Product of Word Lengths
448. Find All Numbers Disappeared in an Array
1155. Number of Dice Rolls With Target Sum
415. Add Strings
22. Generate Parentheses